import select
import socket
import struct
import threading

#data-encapsulation constants
MESSAGE_LANGUAGE = '\x00'

class Socket(threading.Thread):
    _socket = None
    _send_lock = None
    _host = None
    _port = None
    _socket_listeners = None
    _socket_writers = None
    _socket_errors = None
    
    def __init__(self, host, port, ttl):
        threading.Thread.__init__(self)
        
        (self._host, self._port) = address
        self._setup(ttl)
        self._socket_listeners = (self._socket,)
        self._socket_writers = self._socket_errors = ()
        self._send_lock = threading.Lock()
        
    def _setup(self, ttl):
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._socket.bind(('', self._port))
        
        multicast_group = struct.pack("4sl", socket.inet_aton(self._host), socket.INADDR_ANY)
        self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, multicast_group)
        
    def send(self, data):
        with self._send_lock:
            self._socket.sendto(data, (self._host, self._port))
            
    def run(self):
        while True:
            (active, _, _) = select.select([self._], [], [], 60)
            if active:
                packet = active[0].recv(65536)
                if packet[0] == MESSAGE_LANGUAGE:
                    print(packet[1:])
                    
                    
                    


import select
import socket
import struct
import threading



class Socket(threading.Thread):
    
    
    Receive

import socket
import struct

MCAST_GRP = '224.1.1.1'
MCAST_PORT = 5007

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
  print sock.recv(10240)

Send

import socket

MCAST_GRP = '224.1.1.1'
MCAST_PORT = 5007

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto("robot", (MCAST_GRP, MCAST_PORT))


setsockopt(send_s, IPPROTO_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes));

